package com.huarui.inventory.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.service.BaseService;
import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.service.DictionaryReadOnlyService;
import com.huarui.inventory.dao.IInventoryDao;
import com.huarui.inventory.model.Inventory;
import com.huarui.inventory.model.InventoryListModel;
import com.huarui.part.model.Part;
import com.huarui.part.service.PartService;
import com.huarui.sale.util.SaleConstantEnum;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class InventoryService extends BaseService {
	private final static Logger logger = Logger.getLogger(InventoryService.class);
	private IInventoryDao getDao() {
		return (IInventoryDao) dao;
	}

	private DictionaryReadOnlyService dictionaryReadOnlyService;

	private PartService partService;
	
	public void setDictionaryReadOnlyService(
			DictionaryReadOnlyService dictionaryReadOnlyService) {

		this.dictionaryReadOnlyService = dictionaryReadOnlyService;
	}

	@SuppressWarnings("unchecked")
	public InventoryListModel getInventoryByCondition(InventoryListModel lm) {

		lm.setTotal(this.getDao().countByConditions(lm));
		List<Map<String,Object>>   inventoryList= this.getDao()
				.queryInventoryByCondition(lm);

		lm.setResult(inventoryList);
		return lm;

	}

	public List<Map<String, Object>> queryForListBySize(
			Map<String, Object> map, int size) {

		return this.getDao().queryForListBySize(map, size);
	}

	@SuppressWarnings("unchecked")
	public ListModel<Map> getInvetoryForMapByConditions(Map<String, Object> quotaMap, ListModel listModel) {
		 SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		 quotaMap.put(ConstantParamEnum.COMPANYID.getType(), sessionMessage.getCompanyId());
		 quotaMap.put(ConstantParamEnum.VARGROUP.getType(), DictionaryEnum.GROUP_INVENTORY_DOCUMENTYPE.getGroup());
	     listModel.setTotal(getDao().getInventoryCountByCondition(quotaMap));
	     if(listModel.getOrderBy() == null){
	    	 listModel.setOrderBy(ConstantParamEnum.GMTENTRY.getType());
	     }
	     quotaMap.put(SaleConstantEnum.LISTMODEL.getType(), listModel);
	     List<Map> result = getDao().getInventoryForMapByCondition(quotaMap);
	     listModel.setResult(result);	     
		 return listModel;
		
	}
	
	@SuppressWarnings({ "unchecked"})
	public Map<String,Object> getPagingByConditions(Map<String,Object> map,ListModel lm){
		Map<String,Object> resultMap=new HashMap<String, Object>();
		try {
			Integer total = this.getDao().getCountByConditions(map);
			lm.setTotal(total);
			map.put("listModel", lm);
			List<Map<String,Object>> result = this.getDao().getMapByConditions(map);
			lm.setResult(result);
		} catch (Exception e) {
			logger.error("查询库存表出错", e);
		}
		resultMap.put("totalProperty", lm.getTotal());
		resultMap.put("root", lm.getResult());
		return resultMap;
	}
	/**
	 * 销售订单引用列表
	 * 
	 * @param paramsMap
	 * @return
	 */
	public Map<String, Object> showInventoryToSale(Map<String, Object> paramsMap) {
		Integer count = this.getDao().showInventoryToSaleCount(paramsMap);
		List<Map<String, Object>> list = this.getDao().showInventoryToSale(
				paramsMap);
		paramsMap.clear();
		paramsMap.put("totalProperty", count);
		paramsMap.put("root", list);
		return paramsMap;
	}



	@SuppressWarnings("unchecked")
	public Map<String,Object> pickInventoryToSale(String queryParams,Integer start,Integer limit) {
		Map<String,Object> queryMap = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(queryParams)){			
			 queryMap = (Map) JSONObject.toBean(JSONObject.fromObject(queryParams),Map.class);
		}	
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer companyId = sessionMessage.getCompanyId();
		queryMap.put("companyId",companyId);
		Integer count = getDao().getCountByConditions(queryMap);		
		queryMap.put("start", start == null ? 0 :start);
		queryMap.put("pageSize", limit == null ? 20 : limit);
		List<Map<String,Object>> items = getDao().getMapByConditions(queryMap);	
		queryMap.clear();		
		queryMap.put("items", items);							
		queryMap.put("total", count);
		return queryMap;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	

}
